package com.codejob.portal.web.action.company;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.codejob.portal.dao.utils.Page;
import com.codejob.portal.entity.company.Company;
import com.codejob.portal.entity.company.Member;
import com.codejob.portal.services.impl.company.CompanyManager;
import com.codejob.portal.services.impl.company.MemberManager;
import com.codejob.portal.services.intf.BusinessExcption;
import com.codejob.portal.web.BaseModelAction;
import com.codejob.portal.web.utils.Struts2Utils;

@Namespace("/company")
public class MemberAction extends BaseModelAction<Member> {
	private static final long serialVersionUID = -427892708624006754L;
	private Member member;
	private Company company;
	@Autowired
	private MemberManager memManager;
	@Autowired
	private CompanyManager comManager;
	private Long id;
	private Long companyId;
	private Page<Member> page=new Page<Member>(5);
	
	public String prepareApplyJoin(){
		if(!isLogin()){
			return "login";
		}
		company=comManager.getById(companyId);
		Struts2Utils.getRequest().setAttribute("company", company);
		return "applyJoin";
	} 
	
	public String applyJoin(){
		if(!isLogin()){
			return "login";
		}
		try {
			memManager.applyJoin(member,this.getUserId());
		} catch (BusinessExcption e) {
			e.printStackTrace();
		}
		return "applySuccess";
	}
	
	public String join(){
		if(!isLogin()){
			return "login";
		}
		if(this.getUserId()!=member.getCompany().getOwner().getId()){
			return "login";
		}
		memManager.join(member);
		return "joinSuccess";
	}
	
	@Override
	public Member getModel() {
		return member;
	}

	@Override
	public void prepare() throws Exception {
		if(id!=null&&id!=0){
			member=memManager.getById(id);
		}
		if(member==null){
			member=new Member();
		}
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Page<Member> getPage() {
		return page;
	}

	public void setPage(Page<Member> page) {
		this.page = page;
	}

	public Long getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Long companyId) {
		this.companyId = companyId;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	

}
